// 
//  DotNetNuke? - http://www.dotnetnuke.com
//  Copyright (c) 2002-2006
//  by DotNetNuke Corporation
// 
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
//  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
//  of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
//  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//  DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Data;
using System.Collections.Generic;
using DotNetNuke;
using System.Text;
using System.Data.SqlClient;

namespace DotNetNuke.Modules.IWebCSharp
{
    public class IWebController
    {
        public static int CheckIWebSettings(int PortalID, string SettingID)
        {
            int i = 0;
            IWebInfo IWebInfo = new IWebInfo();
            StringBuilder mySqlString = new StringBuilder();
            mySqlString.Append("SELECT Setting ");
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebCSharpSettings ");
            mySqlString.Append("WHERE PortalID = @PortalID ");
            mySqlString.Append("AND SettingID = @SettingID ");
            mySqlString.Append("ORDER BY PortalID ");

            SqlParameter prmPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            SqlParameter prmSettingID = new SqlParameter("@SettingID", SqlDbType.NVarChar, 100);
            prmPortalID.Value = PortalID;
            prmSettingID.Value = SettingID;

            using (IDataReader dr = ((IDataReader)(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID, prmSettingID))))
            {
                while (dr.Read())
                {
                    i++;
                }
            }

            return i;
        }

        public static string GetIWebSettings(int PortalID, string SettingID)
        {
            IWebInfo IWebInfo = new IWebInfo();

            StringBuilder mySqlString = new StringBuilder();
            mySqlString.Append("SELECT Setting ");
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebCSharpSettings ");
            mySqlString.Append("WHERE PortalID = @PortalID ");
            mySqlString.Append("AND SettingID = @SettingID ");

            SqlParameter prmPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            SqlParameter prmSettingID = new SqlParameter("@SettingID", SqlDbType.NVarChar, 100);
            prmPortalID.Value = PortalID;
            prmSettingID.Value = SettingID;

            using (IDataReader dr = ((IDataReader)(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID, prmSettingID))))
            {
                while (dr.Read())
                {
                    IWebInfo.Setting = Convert.ToString(dr["Setting"]);
                }
            }

            return IWebInfo.Setting;
        }


        public static void DeleteIWebSettings(IWebInfo colIWebInfo)
        {
            StringBuilder mySqlString = new StringBuilder();
            mySqlString.Append("DELETE FROM ");
            mySqlString.Append("{objectQualifier}IWebCSharpSettings ");
            mySqlString.Append("WHERE ");
            mySqlString.Append("(PortalID = @PortalID) AND (SettingID = @SettingID) AND (Setting = @Setting) ");

            SqlParameter prmPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            SqlParameter prmSettingID = new SqlParameter("@SettingID", SqlDbType.NVarChar, 100);
            SqlParameter prmSetting = new SqlParameter("@Setting", SqlDbType.NVarChar, 100);
            prmPortalID.Value = colIWebInfo.PortalID;
            prmSettingID.Value = colIWebInfo.SettingID;
            prmSetting.Value = colIWebInfo.Setting;

            DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID, prmSettingID, prmSetting);
        }

        public static void InsertIWebSettings(IWebInfo colIWebInfo)
        {
            StringBuilder mySqlString = new StringBuilder();
            mySqlString.Append("INSERT INTO ");
            mySqlString.Append("{objectQualifier}IWebCSharpSettings ");
            mySqlString.Append("(PortalID, SettingID, Setting) ");
            mySqlString.Append("VALUES ");
            mySqlString.Append("(@PortalID,@SettingID,@Setting) ");

            SqlParameter prmPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            SqlParameter prmSettingID = new SqlParameter("@SettingID", SqlDbType.NVarChar, 100);
            SqlParameter prmSetting = new SqlParameter("@Setting", SqlDbType.NVarChar, 100);
            prmPortalID.Value = colIWebInfo.PortalID;
            prmSettingID.Value = colIWebInfo.SettingID;
            prmSetting.Value = colIWebInfo.Setting;

            DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID, prmSettingID, prmSetting);
        }

        public static void UpdateIWebSettings(IWebInfo colIWebInfo)
        {
            StringBuilder mySqlString = new StringBuilder();
            mySqlString.Append("UPDATE ");
            mySqlString.Append("{objectQualifier}IWebCSharpSettings ");
            mySqlString.Append("SET ");              
            mySqlString.Append("Setting = @Setting ");
            mySqlString.Append("WHERE ");
            mySqlString.Append("(PortalID = @PortalID) AND (SettingID = @SettingID) ");
            
            SqlParameter prmPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            SqlParameter prmSettingID = new SqlParameter("@SettingID", SqlDbType.NVarChar, 100);
            SqlParameter prmSetting = new SqlParameter("@Setting", SqlDbType.NVarChar, 100);
            prmPortalID.Value = colIWebInfo.PortalID;
            prmSettingID.Value = colIWebInfo.SettingID;
            prmSetting.Value = colIWebInfo.Setting;

            DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID, prmSettingID, prmSetting);
        }

    }
}



